<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>系统设置<small>权限管理</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>系统设置</el-breadcrumb-item>
                    <el-breadcrumb-item>权限管理</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-input placeholder="权限名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                        <el-button @click="findPage(1)" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="name" label="权限名称" align="center"></el-table-column>
						<el-table-column prop="keyword" label="权限Key" align="center"></el-table-column>
                        <el-table-column prop="description" label="项目说明" align="center"></el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增用户" :visible.sync="dialogFormVisible">
                            <template>
                                <el-tabs v-model="activeName" type="card">
                                    <el-tab-pane label="基本信息" name="first">
                                        <el-form label-position="right" label-width="100px">
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="权限名称">
                                                        <el-input v-model="formData.name"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="权限Key">
                                                        <el-input v-model="formData.keyword"/>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="权限描述">
                                                        <el-input v-model="formData.description"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>

                                        </el-form>
                                    </el-tab-pane>
                                    <el-tab-pane label="角色信息" name="second">
                                        <div class="checkScrol">
                                            <table class="datatable">
                                                <thead>
                                                <tr>
                                                    <th>选择</th>

                                                    <th>角色名称</th>
                                                </tr>
                                                </thead>
                                                <tbody>
                                                <tr v-for="c in tableData">
                                                    <td>
                                                        <input :id="c.id" v-model="roles" type="checkbox" :value="c.id">
                                                    </td>
                                                    <td><label :for="c.id">{{c.code}}</label></td>
                                                    <td><label :for="c.id">{{c.name}}</label></td>
                                                    <td><label :for="c.id">{{c.remark}}</label></td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </el-tab-pane>
                                </el-tabs>
                            </template>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>

                    <!-- 编辑标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="编辑检查项" :visible.sync="dialogFormVisible4Edit">
                            <template>
                                <el-tabs v-model="activeName" type="card">
                                    <el-tab-pane label="基本信息" name="first">
                                        <el-form label-position="right" label-width="100px">
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="权限名称">
                                                        <el-input v-model="formData.name"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="权限Key">
                                                        <el-input v-model="formData.keyword"/>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="权限描述">
                                                        <el-input v-model="formData.description"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>

                                        </el-form>
                                    </el-tab-pane>
                                    <el-tab-pane label="角色信息" name="second">
                                        <div class="checkScrol">
                                            <table class="datatable">
                                                <thead>
                                                <tr>
                                                    <th>选择</th>

                                                    <th>角色名称</th>
                                                </tr>
                                                </thead>
                                                <tbody>
                                                <tr v-for="c in tableData">
                                                    <td>
                                                        <input :id="c.id" v-model="roles" type="checkbox" :value="c.id">
                                                    </td>
                                                    <td><label :for="c.id">{{c.code}}</label></td>
                                                    <td><label :for="c.id">{{c.name}}</label></td>
                                                    <td><label :for="c.id">{{c.remark}}</label></td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </el-tab-pane>
                                </el-tabs>
                            </template>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script>
        axios.defaults.withCredentials=true;
        var vue = new Vue({
            el: '#app',
            data:{
                backend_url: "http://localhost:9002",
                activeName:'first',
                pagination: {//分页相关模型数据
					  currentPage: 1,//当前页码
					  pageSize:10,//每页显示的记录数
					  total:0,//总记录数
					  queryString:null//查询条件
				},
				dataList: [],//当前页要展示的分页列表数据
                formData: {},//表单数据
                tableData: [],//新增和编辑表单中对应的角色列表数据
                roles: [],//新增和编辑表单中角色对应的复选框，基于双向绑定可以进行回显和数据提交
                dialogFormVisible: false,//增加表单是否可见
                dialogFormVisible4Edit:false,//编辑表单是否可见
                rules: {//校验规则
                    code: [{ required: true, message: '项目编码为必填项', trigger: 'blur' }],
                    name: [{ required: true, message: '项目名称为必填项', trigger: 'blur' }]
                }
            },
            //钩子函数，VUE对象初始化完成后自动执行
            created() {
                //分页查询
                this.findPage();
            },
            methods: {
                //编辑---保存
                handleEdit() {
                    //1.表单校验
                    axios.post(this.backend_url + "/permission/edit?roles="+this.roles, this.formData).then((res) => {
                        if (res.data.flag) {
                            this.$message.success(res.data.message);
                        } else {
                            this.$message.success(res.data.message);
                        }
                    }).catch((error) => {
                        this.$message.error("网络异常")
                    }).finally(() => {
                        this.findPage();
                        this.dialogFormVisible4Edit = false
                    })
                },
                //添加
                handleAdd () {
                    //表单校验
                            //发起http post请求
                            axios.post(this.backend_url + "/permission/add?roles="+this.roles, this.formData).then((response) => {
                                if(response.data.flag){
                                    //请求成功
                                    this.$message.success(response.data.message);
                                }else{
                                    //请求失败
                                    this.$message.error(response.data.message);
                                }
                            }).catch((error)=>{
                                console.log(error);
                                this.$message.error("网络异常");
                            }).finally(()=>{
                                //刷新数据
                                this.findPage();
                                //隐藏新增窗口
                                this.dialogFormVisible=false;
                            });
                },
                //分页查询
                findPage(pageNo) {
                    //1.判断页码是否存在，存在则更新分页数据模型
                    if(pageNo){
                        this.pagination.currentPage = pageNo;
                    }
                    //2.构造请求参数 页码 页大小 查询条件
                    var param = {
                        currentPage: this.pagination.currentPage, //当前页
                        pageSize: this.pagination.pageSize, //页大小
                        queryString: this.pagination.queryString //查询条件
                    };
                    //3.发送请求----》查询数据
                    //发起http get请求
                    axios.get(this.backend_url + "/permission/findPage", {params:param}).then((response) => {
                        if(response.data.flag){
                             //请求成功
                            // 4.将成功请求到的数据渲染
                            //response.data--->Result
                            //response.data.data--->PageResult
                            //数据dataList赋值
                            this.dataList = response.data.data.rows;
                            //4.2分页total赋值
                            this.pagination.total = response.data.data.total;
                        }else{
                            //请求失败
                            this.$message.error(response.data.message);
                        }
                    }).catch((error)=>{
                        console.log(error);
                        this.$message.error("网络异常");
                    });

                },
                // 重置表单
                resetForm(formName) {
                    //重置表单数据
                    this.formData = {};
                    this.roles=[];
                    if(this.$refs[formName]){ //判断是否有子节点
                        //重置子节点数据
                        this.$refs[formName].resetFields();
                    }
                },
                // 弹出添加窗口
                handleCreate() {
                    //清空表单
                    this.resetForm("dialogFormVisible");
                    axios.get(this.backend_url + "/role/all").then((res) => {
                        if (res.data.flag) {
                            this.tableData = res.data.data;
                        }
                    }).catch((error) => {
                        this.$message.error("查询失败");
                    })
                    //弹出添加窗口
                    this.dialogFormVisible = true;
                },
                // 弹出编辑窗口
                handleUpdate(row) {
                    //1.弹出编辑窗口
                    this.dialogFormVisible4Edit = true;
                    //2.根据id查询数据
                    //发起http get请求
                    axios.get(this.backend_url + "/permission/findById?id=" + row.id).then((res) => {
                        if (res.data.flag) {
                            axios.get(this.backend_url + "/role/all").then((respon) => {
                                if (respon.data.flag) {
                                    this.tableData = respon.data.data
                                    axios.get(this.backend_url + "/permission/findByPermissionRoleId?id=" + row.id).then((resp) => {
                                        if (resp.data.flag) {
                                            this.roles = resp.data.data;
                                        } else {
                                            this.$message.success(res.data.message);
                                        }
                                    }).catch((error) => {
                                        this.$message.error("打钩显示异常")
                                    })
                                }
                            }).catch((error) => {
                                this.$message.error("查询显示异常")
                            })
                            this.formData = res.data.data;
                        } else {
                            this.$message.success(res.data.message);
                        }
                    }).catch((error) => {
                        this.$message.error("网络异常")
                    })
                },
                //切换页码
                handleCurrentChange(currentPage) {
                    //分页插件调用分页
                    this.findPage(currentPage);
                },
                // 删除
                handleDelete(row) {
                    //alert(row.id);
                    //弹出提示框
                    this.$confirm("确定要删除吗？", "提示", {type: "waring"}).then(() => {
                        axios.post(this.backend_url+"/permission/delete?id=" + row.id).then((res) => {
                            if (res.data.flag) {
                                this.$message.error(res.data.message);
                                this.findPage();
                            } else {
                                this.$message.error(res.data.message);
                            }
                        }).catch((error) => {
                            this.$message.error("网络异常")
                        })
                    });
                }
            }
        })
    </script>
</html>